package model.factories.environment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
import model.Interpreter;
import model.SyntaxErrorException;
import model.commands.Command;
import model.commands.environment.TellCommand;
import model.factories.CommandFactory;


/**
 * A command factory that creates TellCommand objects.
 * 
 * @author Erick Gonzalez
 */
public class TellCommandFactory extends CommandFactory {


    private final Pattern myTellPattern = Pattern.
            compile(getInterpreter().getLanguage().getString("TellCommandPattern"));

    /**
     * 
     * @param interpreter an interpreter
     */
    public TellCommandFactory (Interpreter interpreter) {
        super(interpreter);
    }

    @Override
    public Command createCommand (Scanner commandReader,
                                  Map<String, Integer> variables) throws SyntaxErrorException {
        List<Integer> turtleIds = new ArrayList<Integer>();

        // read the command keywords
        commandReader.next();

        // read the open bracket
        commandReader.next();

        while (commandReader.hasNextInt()) {
            turtleIds.add(commandReader.nextInt());
        }
        // read closing bracket
        commandReader.next();
        return new TellCommand(turtleIds);
    }

    @Override
    public Pattern patternToMatch () {
        return myTellPattern;
    }

}
